home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / src-server / xlisp / xlprin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  10.5 KB  |  420 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         xlprint.c
  5. * RCS:          $Header: xlprin.c,v 1.8 91/03/24 22:25:20 mayer Exp $
  6. * Description:  xlisp print routine
  7. * Author:       David Michael Betz
  8. * Created:      
  9. * Modified:     Fri Oct  4 04:09:03 1991 (Niels Mayer) mayer@hplnpm
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       X11r5 contrib tape release
  13. *
  14. * WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. *
  17. * Permission to use, copy, modify, distribute, and sell this software and its
  18. * documentation for any purpose is hereby granted without fee, provided that
  19. * the above copyright notice appear in all copies and that both that
  20. * copyright notice and this permission notice appear in supporting
  21. * documentation, and that the name of Hewlett-Packard and David Betz not be
  22. * used in advertising or publicity pertaining to distribution of the software
  23. * without specific, written prior permission.  Hewlett-Packard and David Betz
  24. * make no representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  28. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  29. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  30. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  32. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  33. * PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * See ./winterp/COPYRIGHT for information on contacting the authors.
  36. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  37. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  38. *
  39. ********************************************************************************
  40. */
  41. static char rcs_identity[] = "@(#)$Header: xlprin.c,v 1.8 91/03/24 22:25:20 mayer Exp $";
  42.  
  43.  
  44. #include "xlisp.h"
  45.  
  46. #ifdef WINTERP
  47. extern LVAL Wres_Get_Symbol();    /* w_resources.c */
  48. #endif
  49.  
  50. /* external variables */
  51. extern LVAL tentry();
  52. extern LVAL s_printcase,k_downcase,k_const,k_nmacro;
  53. extern LVAL s_ifmt,s_ffmt;
  54. extern FUNDEF funtab[];
  55. extern char buf[];
  56.  
  57. /* xlprint - print an xlisp value */
  58. xlprint(fptr,vptr,flag)
  59.   LVAL fptr,vptr; int flag;
  60. {
  61.     LVAL nptr,next;
  62.     int n,i;
  63.  
  64.     /* print nil */
  65.     if (vptr == NIL) {
  66.     putsymbol(fptr,"NIL",flag);
  67.     return;
  68.     }
  69.  
  70.     /* check value type */
  71.     switch (ntype(vptr)) {
  72.     case SUBR:
  73.         putsubr(fptr,"Subr",vptr);
  74.         break;
  75.     case FSUBR:
  76.         putsubr(fptr,"FSubr",vptr);
  77.         break;
  78.     case CONS:
  79.         xlputc(fptr,'(');
  80.         for (nptr = vptr; nptr != NIL; nptr = next) {
  81.             xlprint(fptr,car(nptr),flag);
  82.         if (next = cdr(nptr))
  83.             if (consp(next))
  84.             xlputc(fptr,' ');
  85.             else {
  86.             xlputstr(fptr," . ");
  87.             xlprint(fptr,next,flag);
  88.             break;
  89.             }
  90.         }
  91.         xlputc(fptr,')');
  92.         break;
  93.     case SYMBOL:
  94.         putsymbol(fptr,getstring(getpname(vptr)),flag);
  95.         break;
  96.     case FIXNUM:
  97.         putfixnum(fptr,getfixnum(vptr));
  98.         break;
  99.     case FLONUM:
  100.         putflonum(fptr,getflonum(vptr));
  101.         break;
  102.     case CHAR:
  103.         putchcode(fptr,getchcode(vptr),flag);
  104.         break;
  105.     case STRING:
  106.         if (flag)
  107.         putqstring(fptr,vptr);
  108.         else
  109.         putstring(fptr,vptr);
  110.         break;
  111.     case STREAM:
  112.         putatm(fptr,"File-Stream",vptr);
  113.         break;
  114.     case USTREAM:
  115.         putatm(fptr,"Unnamed-Stream",vptr);
  116.         break;
  117.     case OBJECT:
  118.         putatm(fptr,"Object",vptr);
  119.         break;
  120.     case VECTOR:
  121.         xlputc(fptr,'#'); xlputc(fptr,'(');
  122.         for (i = 0, n = getsize(vptr) - 1; i <= n; ++i) {
  123.         xlprint(fptr,getelement(vptr,i),flag);
  124.         if (i != n) xlputc(fptr,' ');
  125.         }
  126.         xlputc(fptr,')');
  127.         break;
  128.     case STRUCT:
  129.         xlprstruct(fptr,vptr,flag);
  130.         break;
  131.     case CLOSURE:
  132.         putclosure(fptr,vptr);
  133.         break;
  134.     case FREE:
  135.         putatm(fptr,"Free",vptr);
  136.         break;
  137.  
  138. #ifdef WINTERP
  139.     case XLTYPE_XtAccelerators:
  140.         putatm(fptr, "XtAccelerators", vptr);
  141.         break;
  142.     case XLTYPE_XtTranslations:
  143.         putatm(fptr, "XtTranslations", vptr);
  144.         break;
  145.     case XLTYPE_XEvent:
  146.         putatm(fptr, "XEvent", vptr);
  147.         break;
  148.     case XLTYPE_Window:
  149.         putatm(fptr, "Window", vptr);
  150.         break;
  151.     case XLTYPE_Pixel:
  152.         putatm(fptr, "Pixel", vptr);
  153.         break;
  154.     case XLTYPE_Pixmap:
  155.         putatm(fptr, "Pixmap", vptr);
  156.         break;
  157.     case XLTYPE_XImage:
  158.         putatm(fptr, "XImage", vptr);
  159.         break;
  160.     case XLTYPE_XmString:
  161.         putatm(fptr, "XmString", vptr);
  162.         break;
  163.     case XLTYPE_XT_RESOURCE:
  164.         putsymbol(fptr,
  165.               getstring(getpname(Wres_Get_Symbol(vptr))),
  166.               flag);
  167.         break;
  168.     case XLTYPE_CALLBACKOBJ:
  169.         putatm(fptr, "CALLBACK-OBJ", vptr);
  170.         break;
  171.     case XLTYPE_TIMEOUTOBJ:
  172.         putatm(fptr, "TIMEOUT-OBJ", vptr);
  173.         break;
  174.     case XLTYPE_PIXMAP_REFOBJ:
  175.         putatm(fptr, "PIXMAP-REFOBJ", vptr);
  176.         break;
  177.     case XLTYPE_WIDGETOBJ:
  178.         Wcls_Print_WIDGETOBJ(fptr, vptr); /* from w_classes.c */
  179.         break;
  180.     case XLTYPE_EVHANDLEROBJ:
  181.         putatm(fptr, "EVHANDLER-OBJ", vptr);
  182.         break;
  183. #endif                /* WINTERP */
  184.  
  185. #if (defined(UNIX) || defined(WINTERP))
  186.     case XLTYPE_PIPE:
  187.         putatm(fptr, "Pipe-Stream", vptr);
  188.         break;
  189. #endif /* (defined(UNIX) || defined(WINTERP)) */
  190.  
  191.     default:
  192.         putatm(fptr,"Foo",vptr);
  193.         break;
  194.     }
  195. }
  196.  
  197. /* xlterpri - terminate the current print line */
  198. xlterpri(fptr)
  199.   LVAL fptr;
  200. {
  201.     xlputc(fptr,'\n');
  202. }
  203.  
  204. /* xlputstr - output a string */
  205. xlputstr(fptr,str)
  206.   LVAL fptr; char *str;
  207. {
  208.     while (*str)
  209.     xlputc(fptr,*str++);
  210. }
  211.  
  212. /* putsymbol - output a symbol */
  213. LOCAL putsymbol(fptr,str,escflag)
  214.   LVAL fptr; char *str; int escflag;
  215. {
  216.     int downcase,ch;
  217.     LVAL type;
  218.     char *p;
  219.  
  220.     /* check for printing without escapes */
  221.     if (!escflag) {
  222.     xlputstr(fptr,str);
  223.     return;
  224.     }
  225.  
  226.     /* check to see if symbol needs escape characters */
  227.     if (tentry(*str) == k_const) {
  228.     for (p = str; *p; ++p)
  229.         if (islower(*p)
  230.         ||  ((type = tentry(*p)) != k_const
  231.           && (!consp(type) || car(type) != k_nmacro))) {
  232.         xlputc(fptr,'|');
  233.         while (*str) {
  234.             if (*str == '\\' || *str == '|')
  235.             xlputc(fptr,'\\');
  236.             xlputc(fptr,*str++);
  237.         }
  238.         xlputc(fptr,'|');
  239.         return;
  240.         }
  241.     }
  242.  
  243.     /* get the case translation flag */
  244.     downcase = (getvalue(s_printcase) == k_downcase);
  245.  
  246.     /* check for the first character being '#' */
  247.     if (*str == '#' || *str == '.' || isnumber(str,NULL))
  248.     xlputc(fptr,'\\');
  249.  
  250.     /* output each character */
  251.     while ((ch = *str++) != '\0') {
  252.     /* don't escape colon until we add support for packages */
  253.     if (ch == '\\' || ch == '|' /* || ch == ':' */)
  254.         xlputc(fptr,'\\');
  255.     xlputc(fptr,(downcase && isupper(ch) ? tolower(ch) : ch));
  256.     }
  257. }
  258.  
  259. /* putstring - output a string */
  260. LOCAL putstring(fptr,str)
  261.   LVAL fptr,str;
  262. {
  263.     unsigned char *p;
  264.     int ch;
  265.  
  266.     /* output each character */
  267.     for (p = getstring(str); (ch = *p) != '\0'; ++p)
  268.     xlputc(fptr,ch);
  269. }
  270.  
  271. /* putqstring - output a quoted string */
  272. LOCAL putqstring(fptr,str)
  273.   LVAL fptr,str;
  274. {
  275.     unsigned char *p;
  276.     int ch;
  277.  
  278.     /* get the string pointer */
  279.     p = getstring(str);
  280.  
  281.     /* output the initial quote */
  282.     xlputc(fptr,'"');
  283.  
  284.     /* output each character in the string */
  285.     for (p = getstring(str); (ch = *p) != '\0'; ++p)
  286.  
  287.     /* check for a control character */
  288.     if (ch < 040 || ch == '\\' || ch > 0176) {
  289.         xlputc(fptr,'\\');
  290.         switch (ch) {
  291.         case '\011':
  292.             xlputc(fptr,'t');
  293.             break;
  294.         case '\012':
  295.             xlputc(fptr,'n');
  296.             break;
  297.         case '\014':
  298.             xlputc(fptr,'f');
  299.             break;
  300.         case '\015':
  301.             xlputc(fptr,'r');
  302.             break;
  303.         case '\\':
  304.             xlputc(fptr,'\\');
  305.             break;
  306.         default:
  307.             putoct(fptr,ch);
  308.             break;
  309.         }
  310.     }
  311.  
  312.     /* output a normal character */
  313.     else
  314.         xlputc(fptr,ch);
  315.  
  316.     /* output the terminating quote */
  317.     xlputc(fptr,'"');
  318. }
  319.  
  320. /* putatm - output an atom */
  321. LOCAL putatm(fptr,tag,val)
  322.   LVAL fptr; char *tag; LVAL val;
  323. {
  324.     sprintf(buf,"#<%s: #",tag); xlputstr(fptr,buf);
  325.     sprintf(buf,AFMT,val); xlputstr(fptr,buf);
  326.     xlputc(fptr,'>');
  327. }
  328.  
  329. /* putsubr - output a subr/fsubr */
  330. LOCAL putsubr(fptr,tag,val)
  331.   LVAL fptr; char *tag; LVAL val;
  332. {
  333.     sprintf(buf,"#<%s-%s: #",tag,funtab[getoffset(val)].fd_name);
  334.     xlputstr(fptr,buf);
  335.     sprintf(buf,AFMT,val); xlputstr(fptr,buf);
  336.     xlputc(fptr,'>');
  337. }
  338.  
  339. /* putclosure - output a closure */
  340. LOCAL putclosure(fptr,val)
  341.   LVAL fptr,val;
  342. {
  343.     LVAL name;
  344.     if (name = getname(val))
  345.     sprintf(buf,"#<Closure-%s: #",getstring(getpname(name)));
  346.     else
  347.     strcpy(buf,"#<Closure: #");
  348.     xlputstr(fptr,buf);
  349.     sprintf(buf,AFMT,val); xlputstr(fptr,buf);
  350.     xlputc(fptr,'>');
  351. /*
  352.     xlputstr(fptr,"\nName:   "); xlprint(fptr,getname(val),TRUE);
  353.     xlputstr(fptr,"\nType:   "); xlprint(fptr,gettype(val),TRUE);
  354.     xlputstr(fptr,"\nLambda: "); xlprint(fptr,getlambda(val),TRUE);
  355.     xlputstr(fptr,"\nArgs:   "); xlprint(fptr,getargs(val),TRUE);
  356.     xlputstr(fptr,"\nOargs:  "); xlprint(fptr,getoargs(val),TRUE);
  357.     xlputstr(fptr,"\nRest:   "); xlprint(fptr,getrest(val),TRUE);
  358.     xlputstr(fptr,"\nKargs:  "); xlprint(fptr,getkargs(val),TRUE);
  359.     xlputstr(fptr,"\nAargs:  "); xlprint(fptr,getaargs(val),TRUE);
  360.     xlputstr(fptr,"\nBody:   "); xlprint(fptr,getbody(val),TRUE);
  361.     xlputstr(fptr,"\nEnv:    "); xlprint(fptr,getenv(val),TRUE);
  362.     xlputstr(fptr,"\nFenv:   "); xlprint(fptr,getfenv(val),TRUE);
  363. */
  364. }
  365.  
  366. /* putfixnum - output a fixnum */
  367. LOCAL putfixnum(fptr,n)
  368.   LVAL fptr; FIXTYPE n;
  369. {
  370.     char *fmt;            /* NPM: changed from unsigned char* to prevent compiler warning */
  371.     LVAL val;
  372.     fmt = ((val = getvalue(s_ifmt)) && stringp(val) ? (char *) getstring(val)
  373.                             : (char *)IFMT);
  374.     sprintf(buf,fmt,n);
  375.     xlputstr(fptr,buf);
  376. }
  377.  
  378. /* putflonum - output a flonum */
  379. LOCAL putflonum(fptr,n)
  380.   LVAL fptr; FLOTYPE n;
  381. {
  382.     char *fmt;            /* NPM: changed from unsigned char* to prevent compiler warning */
  383.     LVAL val;
  384.     fmt = ((val = getvalue(s_ffmt)) && stringp(val) ? (char *) getstring(val)
  385.                             : (char *)"%g");
  386.     sprintf(buf,fmt,n);
  387.     xlputstr(fptr,buf);
  388. }
  389.  
  390. /* putchcode - output a character */
  391. LOCAL putchcode(fptr,ch,escflag)
  392.   LVAL fptr; int ch,escflag;
  393. {
  394.     if (escflag) {
  395.     switch (ch) {
  396.     case '\n':
  397.         xlputstr(fptr,"#\\Newline");
  398.         break;
  399.     case ' ':
  400.         xlputstr(fptr,"#\\Space");
  401.         break;
  402.     default:
  403.         sprintf(buf,"#\\%c",ch);
  404.         xlputstr(fptr,buf);
  405.         break;
  406.     }
  407.     }
  408.     else
  409.     xlputc(fptr,ch);
  410. }
  411.  
  412. /* putoct - output an octal byte value */
  413. LOCAL putoct(fptr,n)
  414.   LVAL fptr; int n;
  415. {
  416.     sprintf(buf,"%03o",n);
  417.     xlputstr(fptr,buf);
  418. }
  419.